from django_filters import *
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from rest_framework.filters import SearchFilter, OrderingFilter

from mysqldb.models import *

# class AllinpayPlaceinterLogFilterSet(FilterSet):
#     """员工积分日志过滤器"""

#     pid = NumberFilter(field_name="pid", required=False, label="场所ID")
#     sid = NumberFilter(field_name="sid", required=False, label="员工ID")
#     intertype = NumberFilter(field_name="intertype", required=False, label="积分类型ID")
#     strdate = DateTimeFilter(
#         field_name="currenttime", required=False, method="filter_strdate", label="开始时间"
#     )
#     enddate = DateTimeFilter(
#         field_name="currenttime", required=False, method="filter_enddate", label="结束时间"
#     )
#     status = NumberFilter(field_name="status", required=False, label="状态")
#     post = CharFilter(field_name="post", required=False, label="兑换单")

#     def filter_strdate(self, qs, name, value):
#         lookup = "__".join([name, "gte"])
#         return qs.filter(**{lookup: value})

#     def filter_enddate(self, qs, name, value):
#         lookup = "__".join([name, "lte"])
#         return qs.filter(**{lookup: value})


#     class Meta:
#         model = AllinpayPlaceinterLog
#         fields = ["pid", "sid", "intertype", "strdate", "enddate", "status", "post"]


class AllinpayRobotReqsnFilterSet(FilterSet):
    """小程序充值订单过滤器"""

    answer = NumberFilter(field_name="answer", required=False, help_text="用户ID")
    mark = NumberFilter(field_name="mark", required=False, help_text="收付标志")
    openid = CharFilter(field_name="openid", required=False, help_text="小程序openid")
    starttime = DateTimeFilter(
        field_name="lasttime",
        required=False,
        method="filter_strdate",
        help_text="开始时间",
    )
    endtime = DateTimeFilter(
        field_name="lasttime",
        required=False,
        method="filter_enddate",
        help_text="结束时间",
    )

    def filter_strdate(self, qs, name, value):
        lookup = "__".join([name, "gte"])
        return qs.filter(**{lookup: value})

    def filter_enddate(self, qs, name, value):
        lookup = "__".join([name, "lte"])
        return qs.filter(**{lookup: value})

    class Meta:
        model = AllinpayRobotReqsn
        fields = ["answer", "mark", "openid", "starttime", "endtime"]


class AllinpayRobotConfigFilterSet(FilterSet):
    """ai模型列表过滤"""

    status = NumberFilter(
        field_name="status",
        method="filter_status",
        required=False,
        help_text="AI模型训练状态 0|1",
    )
    gender = NumberFilter(
        field_name="gender", required=False, help_text="AI模型性别 0|1"
    )
    answer = NumberFilter(
        field_name="answer", required=False, help_text="订阅模板创建者"
    )
    language = CharFilter(
        field_name="language",
        required=False,
        help_text="AI模型语言 'Chinese', 'English', 'Japanese', '中英混合', '日英混合', '多语种混合'",
    )
    scope = NumberFilter(
        field_name="id",
        method="filter_scope",
        required=False,
        help_text="是否公有(default=0) 0:private | 1:public ",
    )

    def filter_status(self, qs, name, value):
        if value:
            lookup = "__".join([name, "gt"])
            return qs.filter(**{lookup: 0})
        else:
            return qs.filter(**{name: 0})

    def filter_scope(self, qs, name, value):
        if value:
            public = AllinpayRobotConfigShare.objects.filter(status=1).values_list(
                "id", flat=True
            )
            lookup = "__".join([name, "in"])
            return qs.filter(**{lookup: public})
        else:
            return qs

    class Meta:
        model = AllinpayRobotConfig
        fields = ["status", "gender", "answer", "language"]


class AllinpayRobotListFilterSet(FilterSet):
    """私密好友列表过滤"""

    isbot = NumberFilter(field_name="isbot", required=False, help_text="声音模型ID")
    status = NumberFilter(field_name="status", required=False, help_text="状态 0|1")
    parent = NumberFilter(field_name="parent", required=False, help_text="创建者")
    gender = NumberFilter(field_name="gender", required=False, help_text="性别 0|1")
    share = NumberFilter(
        field_name="share",
        method="filter_share",
        required=False,
        help_text="共享状态 0|1",
    )

    def filter_share(self, qs, name, value):
        if value:
            lookup = "__".join([name, "gt"])
            return qs.filter(**{lookup: 0})
        else:
            return qs.filter(**{name: 0})

    class Meta:
        model = AllinpayRobotList
        fields = ["isbot", "status", "parent", "share", "gender"]


class AllinpayRobotModelsFilterSet(FilterSet):
    """订阅模板列表过滤"""

    mode = CharFilter(field_name="mode", required=False, help_text="订阅模板类型")
    answer = NumberFilter(
        field_name="answer", required=False, help_text="订阅模板创建者"
    )
    status = NumberFilter(
        field_name="status", required=False, help_text="订阅模板启用状态 0|1"
    )
    scope = NumberFilter(
        field_name="id",
        method="filter_scope",
        required=False,
        help_text="是否公有(default=0) 0:private | 1:public ",
    )

    class Meta:
        model = AllinpayRobotModels
        fields = ["mode", "answer", "status"]

    def filter_scope(self, qs, name, value):
        if value:
            public = AllinpayRobotModelsShare.objects.filter(status=1).values_list(
                "id", flat=True
            )
            lookup = "__".join([name, "in"])
            return qs.filter(**{lookup: public})
        else:
            return qs


class AllinpayRobotQiniuLogFilterSet(FilterSet):
    """历史任务查询"""

    mode = CharFilter(field_name="mode", required=False, help_text="订阅模板类型")
    guid = CharFilter(field_name="guid", required=False, help_text="GUID")
    answer = NumberFilter(field_name="answer", required=False, help_text="用户ID")
    status = NumberFilter(field_name="status", required=False, help_text="状态")
    model = NumberFilter(field_name="model", required=False, help_text="模型ID")
    mate = NumberFilter(field_name="mate", required=False, help_text="好友ID")
    matr = NumberFilter(field_name="matr", required=False, help_text="是否公共好友")
    sell = NumberFilter(field_name="sell", required=False, help_text="价格")
    subscribe = NumberFilter(
        field_name="mode",
        method="filter_mode",
        required=False,
        help_text="是否订阅 0|1",
    )

    class Meta:
        model = AllinpayRobotQiniuLog
        fields = ["mode", "guid", "answer", "status", "model", "mate", "matr", "sell"]

    def filter_mode(self, qs, name, value):
        """是否订阅"""
        if value:
            public = ["content", "Voice", "Mpeg", "Lipsync"]
            lookup = "__".join([name, "in"])
            return qs.filter(**{lookup: public})
        else:
            return qs
